
SUB = stack
CC = gcc
AR = ar
TARGET = main
RM = -rm -f

SOURCES = $(wildcard $(SUB)/*.c)
OBJECTS = stack.o push.o pop.o is_empty.o
LIB = libstack.so

$(TARGET) : $(OBJECTS)
	$(CC) -c -g -fPIC $(SOURCES)
	$(CC) -shared -o $(LIB) $?
	$(CC) main.c -g -L. -l$(SUB) -I$(SUB) -o $(TARGET) \
		-Wl,-rpath,$(PWD)

#$(OBJECTS) : $(SUB)/stack.h
-include $(SOURCES:.c=.d)
%.d : %.c
	set -e; rm -f $@; \
	$(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

.PHONY : clean
clean : 
	$(RM) $(TARGET) $(OBJECTS) $(LIB)


